package print;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.swing.JPanel;

public class PanelPrinter implements Printable {
	// private final Printable tablePrintable;
	private PageFormat pageFormatJTable;
	private final String subTitle;
	private final int headerspace = 60;
	private final int footerspace = 90;
	private final int border = 15;
	private final JPanel toPrint;

	public PanelPrinter(final JPanel panel, String subTitle) {
		this.toPrint = panel;
		// this.tablePrintable = table.getPrintable(JTable.PrintMode.FIT_WIDTH,
		// null, null);
		this.subTitle = subTitle;
	}

	public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex) throws PrinterException {
		System.out.println("TablePrinter: print() called");

		/**
		 * set up the page
		 */

		Graphics2D graphics2D = (Graphics2D) graphics;
		int leftmostImageable = (int) pageFormat.getImageableX();
		int topmostImageable = (int) pageFormat.getImageableY();
		int imageableWidth = (int) pageFormat.getImageableWidth();
		int imageableHeight = (int) pageFormat.getImageableHeight();
		if (this.pageFormatJTable == null) {
			this.pageFormatJTable = (PageFormat) pageFormat.clone();
			Paper paperJTable = this.pageFormatJTable.getPaper();
			if (this.pageFormatJTable.getOrientation() == PageFormat.PORTRAIT) {
				paperJTable.setImageableArea(leftmostImageable,
				/* skip header area */
				topmostImageable + this.headerspace, imageableWidth,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace);
			} else {
				paperJTable.setImageableArea(
				/* skip header area */
				topmostImageable + this.headerspace, leftmostImageable,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace, imageableWidth);
			}
			this.pageFormatJTable.setPaper(paperJTable);
		}

		Font font = graphics2D.getFont();

		/**
		 * set up title
		 */

		// String title =
		// TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName();
		// title += " (" +
		// TournamentData.getInstance().getGeneralTournamentInformation().getStartDate()
		// + ")";
		// /* increase size of header */
		// graphics2D.setFont(graphics2D.getFont().deriveFont(15f));
		// FontMetrics fontMetrics = graphics2D.getFontMetrics();
		// graphics2D.drawString(title, leftmostImageable, topmostImageable +
		// this.border);
		// // graphics2D.drawString(title, rightmostImageable + ((imageableWidth
		// -
		// // fontMetrics.stringWidth(title)) / 2), topmostImageable + 15);

		/**
		 * set up subtitle
		 */

		// String subtitle = this.subTitle;
		// graphics2D.setFont(font);
		// fontMetrics = graphics2D.getFontMetrics();
		// graphics2D.drawString(subtitle, leftmostImageable, topmostImageable +
		// 30);

		/**
		 * set up ArComp logo
		 */
		// int logoWidth = 100;
		// int logoHeight = 50;
		// graphics2D.drawImage(CustomIcons.logoImage, (leftmostImageable +
		// imageableWidth) - logoWidth, topmostImageable, logoWidth, logoHeight,
		// Color.WHITE, null);
		// graphics2D.drawRect(leftmostImageable, topmostImageable,
		// imageableWidth, 40);

		/**
		 * set up footer
		 */
		// String pageNumber = "Seite " + (pageIndex + 1);
		// graphics2D.drawString(pageNumber, leftmostImageable,
		// (topmostImageable + imageableHeight));
		//
		// // add printing date
		// String DATE_FORMAT_NOW = "dd.MM.yyyy, HH:mm:ss";
		// Calendar cal = Calendar.getInstance();
		// SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		// String date = sdf.format(cal.getTime());
		// graphics2D.drawString(date, (leftmostImageable + imageableWidth) -
		// fontMetrics.stringWidth(date), (topmostImageable + imageableHeight));

		/**
		 * insert the table:
		 */
		// Graphics gCopy = graphics2D.create();

		// graphics2D.translate(pageFormat.getImageableX(),
		// pageFormat.getImageableY());
		// System.out.println("before: " + pageIndex);

		this.toPrint.paint(graphics2D);
		// this.tablePrintable.print(gCopy, this.pageFormatJTable, pageIndex);
		// System.out.println("after: " + pageIndex);
		// gCopy.dispose();

		return PAGE_EXISTS;
	}
}
